package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.storage.bo.StorageReq;
import com.cskaoyan.bean.storage.vo.CreateStorageVo;
import com.cskaoyan.bean.storage.vo.StorageData;
import com.cskaoyan.service.FileService;
import com.cskaoyan.service.StorageService;
import com.cskaoyan.util.StringUtils;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;


@Data
@RestController
//@RequestMapping("admin/storage")//接微信端请求了，不能窄化了
@ConfigurationProperties(prefix = "mall")//这里飘红不影响
public class AdminStorageController {
    @Autowired
    private StorageService storageService;
    @Autowired
    private FileService fileService;
    
    private String storageBasePath;//本地存储文件路径

    private String domain;//域名
    //图片访问url映射前缀
    //url = domain + staticPathPatternPrefix + 散列图片名.后缀
    private String staticPathPatternPrefix;

    private String storageWebAddressPrefix;//使用阿里云后的网络地址前缀

    @GetMapping("admin/storage/list")
    public BaseRespVo storageList(StorageReq req) {
        StorageData storageData = storageService.getStorageList(req);
        return BaseRespVo.ok(storageData);
    }

    @PostMapping("admin/storage/delete")
    public BaseRespVo deleteStorage(@RequestBody Storage storage) {
        storageService.deleteStorage(storage);
        return BaseRespVo.ok();

    }

    /**
     * 微信端和后台管理端公用
     * 文件上传 对象存储
     * 路径配置 见成员变量说明，具体参数见application.yml
     * @param file
     * @return
     */

    /**
     *  操作 : 将变量 storagekey 命名更改为 storageKey
     *
     *  更改人 : 王得昭
     */
    @PostMapping({"admin/storage/create", "wx/storage/upload"})
    public BaseRespVo createStorage(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//文件名
        String contentType = file.getContentType();//类型
        long size = file.getSize();

        String storageKey = StringUtils.hashFileName(originalFilename);
        //阿里云存储实现
        InputStream in = file.getInputStream();
        fileService.fileUpload(storageKey, in);

        //本地存储实现
        /*File fileToUpload = new File(storageBasePath, storageKey);
        File parentFile = fileToUpload.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        try {
            file.transferTo(fileToUpload);
        } catch (IOException e) {
            e.printStackTrace();
            return BaseRespVo.fail("上传失败");
        }
        */
        Storage storage = new Storage();
        storage.setKey(storageKey);
        storage.setName(originalFilename);
        storage.setType(contentType);
        storage.setSize((int) size);
//        storage.setUrl(domain + staticPathPatternPrefix + storageKey);//用于本地储存文件用的url
        storage.setUrl(storageWebAddressPrefix + storageKey);//aliyun图片url
        int code = storageService.createStorage(storage);
        if (code != 0) {
            return BaseRespVo.fail("上传成功，登记失败");
        }
        CreateStorageVo createStorageVo = new CreateStorageVo();
        BeanUtils.copyProperties(storage, createStorageVo);
        return BaseRespVo.ok(createStorageVo);
    }

    /**
     * 修改图片名字
     * 只有name id 属性有用
     * @param storage
     *@param bindingResult
     * @return
     *///
    @PostMapping("admin/storage/update")
    public BaseRespVo updateStorage(@Validated @RequestBody Storage storage, BindingResult bindingResult) {
        int code = storageService.updateStorage(storage);
        if (code != 0) {
            return BaseRespVo.fail("修改失败");
        }
        //属性已经更新了
        return BaseRespVo.ok(storage);
    }
}
